/
EMODnet Chemistry [Points]_sl_plasticbags_760.rmd
363 lines (314 loc) · 13.3 KB
/
EMODnet Chemistry [Points]_sl_plasticbags_760.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
---
title: "Dataset: Seabed litter - Plastic bags density (Nb. Items/km^2)"
author: Document produced by http://www.marine-analyst.eu
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
html_document:
df_print: paged
number_section: yes
theme: default
toc: yes
toc_depth: 2
toc_float:
collapsed: no
smooth_scroll: yes
---
```{r setup, include=FALSE}
library(knitr)
library(kableExtra)
knitr::opts_chunk$set(
eval = TRUE,
echo = TRUE,
fig.align = "center",
message = FALSE,
warning = FALSE,
fig.width=5.5,
out.width = "100%"
)
# clean environment
rm(list=ls())
gc()
```
```{r, include=FALSE, results='hide'}
# Edit the longitude and latitude coordinates to define the geographical area:
minlon=11.3 #minimum longitude
minlat=53.6 #minimum latitude
maxlon=15.5 #maximum longitude
maxlat=55.9 #maximum latitude
wdpaid=paste(minlon,minlat,maxlon,maxlat,sep="_")
Sessionid <- 'test'
wdpaidsplit <- unlist(strsplit(wdpaid, "[_]"))
xmin <- as.numeric(wdpaidsplit[1])
ymin <- as.numeric(wdpaidsplit[2])
xmax <- as.numeric(wdpaidsplit[3])
ymax <- as.numeric(wdpaidsplit[4])
```
```{r, include=FALSE, results='hide'}
source_provider <- "EMODnet Chemistry"source_provider_url <- "https://www.emodnet.eu"layer_title<-"Seabed litter - Plastic bags density"layer="sl_plasticbags"wfs_url <- "https://www.ifremer.fr/services/wfs/emodnet_chemistry2?"wms_url <- "https://www.ifremer.fr/services/wms/emodnet_chemistry2?"wms_layer="sl_plasticbags"layer_id<-760map_legend <- "littercount_by_squarekm"map_label<-"surveyname"link_csv<-paste0("./Report-", layer_id, "_", Sessionid, "_", wdpaid, "-csvfile.csv",sep="")csvfile_name = paste("Report-", layer_id, "_", Sessionid, "_", wdpaid, "-csvfile.csv",sep="")link_geojson<-paste0("./Report-", layer_id, "_", Sessionid, "_", wdpaid, "-geojsonfile.geojson",sep="")geojsonfile_name = paste("Report-", layer_id, "_", Sessionid, "_", wdpaid, "-geojsonfile.geojson",sep="")temp_path<- "."
```
```{r, include=FALSE, results='hide'}
library(rgdal)library(downloader)library(ggplot2)library(mapdata)library(ggmap)library(ggrepel)library(httr)library(sf)library(rasterVis)library(rgeos)library(sp)library(raster)library(dplyr)library(XML)
```
# Data information
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100%><TR><TD WIDTH=100% VALIGN=TOP><DIV ALIGN=JUSTIFY class=><DIV id=standard>This visualisation product displays plastic bags density per trawl per year. EMODnet Chemistry included the gathering of marine litter in its 3rd phase. Since the beginning of 2018, data of seafloor litter collected by international fish-trawl surveys have been gathered and processed in the EMODnet Chemistry Marine Litter Database (MLDB). The harmonization of all the data has been the most challenging task considering the heterogeneity of the data sources, sampling protocols (OSPAR and MEDITS protocols) and reference lists used on a European scale. Moreover, within the same protocol, different gear types are deployed during fishing bottom trawl surveys. Densities have been calculated on each trawl and year using the following computation: Density of plastic bags (number of items per Km^2) = (total number of fishing related items)/(Swept area) Percentiles 50, 75 & 95 have been calculated taking into account data from all years. Plastic bags reference codes taken into account for this product and information on data processing and calculation are detailed in the following document p22.: https://www.emodnet-chemistry.eu/repository/20190321_EMODnet_Beach_Seabed_ML_Products.pdf</DIV></DIV></TD></TR></TABLE>
```{r, include=FALSE, results='hide'}
# Script for Wekeo environment
sr=SpatialPolygons(list(Polygons(list(Polygon(cbind(c(xmin, xmin, xmax, xmax),c(ymax, ymin, ymin, ymax)))),"1")))
mpa=SpatialPolygonsDataFrame(sr, data.frame(cbind(1:1), row.names=c("1")))
proj4string(mpa)<-CRS("+proj=longlat +datum=WGS84")
bbox<-paste(xmin,ymin,xmax,ymax,sep=",")
```
```{r, include=FALSE, results='hide'}
# Link to Marine Analyst dataset page
link_marineanalyst <- paste0("http://marine-analyst.eu/dev.py?N=simple&O=",layer_id,"&maxlat=",ymax,"&maxlon=",xmax,"&minlon=",xmin,"&minlat=",ymin)
# Link to open the openlayer page for EMODnet HA
openlayer<-paste0("http://www.marine-analyst.eu/openlayers3/openlayer.py?wms_url=",wms_url,"/wms&wms_layer=",layer,"&bbox=",bbox)
```
## Metadata
Access <A HREF=`r toString(link_marineanalyst)` TARGET=_blank>metadata</A> from landing page<br>
# Geographical extent
## Coordinates
<A HREF=`r toString(link_marineanalyst)` TARGET=_blank>
```{r,echo=FALSE}
print (paste("West-Longitude:",round(xmin,2)))
print (paste("South-Latitude:",round(ymin,2)))
print (paste("East-Longitude:",round(xmax,2)))
print (paste("North-Latitude:",round(ymax,2)))
```
</a>
## Defined area
```{r ,echo=FALSE}
value<-(xmax-xmin)*(ymax-ymin)
if (value > 100) {
zoom_value<-6
} else if (value > 1) {
zoom_value<-7
} else {
zoom_value<-8
}
base<-get_map(location=c(xmin-1,ymin-1,xmax+1,ymax+1), zoom=zoom_value, maptype="terrain-background", source = "stamen")
terrain <- ggmap(base)
map <- terrain + geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill="mpa"),colour="green",fill="blue",alpha=.1) +
ggtitle("")+xlab("Longitude")+ylab("Latitude")
plot(map)
```
Map tiles by <a href="http://stamen.com">Stamen Design</a>, under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://www.openstreetmap.org/copyright">ODbL</a>.
# `r toString(layer_title)`
## Access data
The Web Feature Service (WFS) of the `r toString(source_provider)` portal allows collecting the data:<BR><A HREF=`r toString(wfs_url)`service=WFS&request=GetCapabilities&version=1.1.0 TARGET=_blank>`r toString(wfs_url)`service=WFS&request=GetCapabilities&version=1.1.0</A><BR><BR>**Available labels:**
```{r,echo=FALSE}
# DescribeFeatureType request functionDescribeFeatureType<-function(layer){layer<-as.character(layer)con<-paste0(wfs_url,"service=WFS&version=1.1.0&request=DescribeFeatureType&typeName=",layer,"&outpuformat=XMLSCHEMA")xml <- "file.xml"xml <- tempfile(xml)httr::GET(con,write_disk(xml))xmldoc <- XML::xmlParse(xml)xml_data <- XML::xmlToList(xmldoc)data.catalog <- data.frame(t(xml_data$complexType$complexContent$extension$sequence),row.names=NULL)return(data.catalog)}WFS_DescribeFeatureType <- DescribeFeatureType(layer)WFS_Colnames<-c()for (i in 1:ncol(WFS_DescribeFeatureType)) {WFS_Colnames<-append(WFS_Colnames, WFS_DescribeFeatureType[i]$element$element[1][["name"]])}WFS_Colnamesrez_nblist<- c(1:length(WFS_Colnames))
```
```{r, include=FALSE, results='hide'}
getWFSgml3<-function(layer){layer<-as.character(layer)con<-paste0(wfs_url,"service=WFS&version=1.0.0&request=GetFeature&typeName=",layer,"&OUTPUTFORMAT=gml3&srsName=EPSG%3A4326")pipo<-sf::st_read(con)return(pipo)}wfs_data<-getWFSgml3(layer)#Transform mpa in Simple feature collection to perform the subsetting because wfs_data contains the whole info - use bbox and tyname are exclusive (Mapserver)mpaSP <- as(mpa, "SpatialPolygonsDataFrame")wfs_data<-wfs_data[st_as_sf(mpaSP),]# get the geometry as lat and lon colswfs_data <- wfs_data %>% dplyr::mutate(lat = sf::st_coordinates(.)[,1],lon = sf::st_coordinates(.)[,2])
```
```{r, include=FALSE}
st_write(wfs_data, file.path(temp_path,csvfile_name), layer = csvfile_name, driver = "csv", delete_dsn = TRUE)
st_write(wfs_data, file.path(temp_path,geojsonfile_name), layer = geojsonfile_name, driver = "GeoJSON", delete_dsn = TRUE)
```
<br>**Download data for the defined geographical extent:**<br><TABLE BORDER=1 WIDTH=100%><TR><TD ALIGN=CENTER>Excel file</TD><TD ALIGN=CENTER>Geographic information</TD></TR><TR><TD ALIGN=CENTER><A HREF=`r toString(link_csv)`>**csv**</A></TD><TD ALIGN=CENTER><A HREF=`r toString(link_geojson)` TARGET=_blank>**geojson**</A></TD></TR></TABLE>
## Table
Browse table's columns by using the left and right arrows. Turn the table's pages with help of the previous/next buttons.<br><br>
```{r ,echo=FALSE}
if(nrow(wfs_data) > 0) {
wfs_data
} else {
print("No data available for the defined geographical extent")
}
```
## Map
```{r,echo=FALSE}
if(nrow(wfs_data) > 0) {map <- ggplot() + borders("worldHires", fill = "gray", colour = "black", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +theme(legend.position = "bottom") + theme(panel.grid.minor.y= element_blank(), panel.grid.minor.x = element_blank()) + geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill="mpa"),colour="green",fill="blue",alpha=.1) + geom_sf() + geom_point(data = wfs_data, aes(x = lat, y = lon, size =.data[[map_legend]]), fill = "red", color = "red", alpha = .4) + coord_sf(xlim = c(xmin, xmax),ylim = c(ymin, ymax))+ ggtitle(layer_title)+xlab("Longitude (x)")+ylab("Latitude (y)")map} else {print("No data available for the defined geographical extent")}
```
## Map with id
```{r,echo=FALSE}
if(nrow(wfs_data) > 0) {centroid<- st_centroid(wfs_data)centroid<- cbind(wfs_data, st_coordinates(st_centroid(wfs_data$geometry)))map <- ggplot() + borders("worldHires", fill = "gray", colour = "black", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +theme(legend.position = "bottom") + theme(panel.grid.minor.y= element_blank(), panel.grid.minor.x = element_blank()) + geom_polygon(data=mpa,aes(x=long,y=lat,group=group,fill="mpa"),colour="green",fill="blue",alpha=.1) + geom_sf() + geom_point(data = wfs_data, aes(x = lat, y = lon, size = .data[[map_legend]]), fill = "red", color = "red", alpha = .4) + geom_text(data=centroid,aes(x=lat, y=lon, label=.data[[map_label]]), color = "black", fontface = "bold", size=2, hjust= 0, vjust=2, check_overlap = TRUE) + coord_sf(xlim = c(xmin, xmax),ylim = c(ymin, ymax))+ ggtitle(layer_title)+xlab("Longitude (x)")+ylab("Latitude (y)")map} else {print("No data available for the defined geographical extent")}
```
## Interactive map
Visualise and access data with <A HREF=`r toString(openlayer)` TARGET=_blank>Openlayer (click on the map)<BR>
```{r, include=FALSE}
if(nrow(wfs_data) > 0) {
getWMSmap<-function (wms_layer,xmin,xmax,ymin,ymax)
{
width <- 960
height <- as.integer(width * (ymax-ymin) / (xmax-xmin))
wms_layer<-as.character(wms_layer)
bbox <- paste(xmin, ymin, xmax, ymax, sep = ",")
con<-paste0(wms_url,"/wms?SERVICE=WMS&VERSION=1.1.0&request=GetMap&layers=",wms_layer,"&format=image/jpeg&srs=EPSG:4326&bbox=",bbox,"&height=",height,"&width=",width,"")
wms <- "img.png"
wms <- tempfile(wms)
download(con, wms, quiet = TRUE, mode = "wb")
img <- brick(wms)
names(img) <- c("img.1", "img.2", "img.3")
img[img$img.1 == 255 & img$img.2 == 255 & img$img.3 == 255] <- NA
wms_basemap_url="http://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv"
wms_basemap_layer="gebco_latest"
con<-paste0(wms_basemap_url,"?SERVICE=WMS&VERSION=1.1.0&request=GetMap&layers=",wms_basemap_layer,"&format=image/png&srs=EPSG:4326&bbox=",bbox,"&height=",height,"&width=",width,"")
wms <- "img.png"
wms <- tempfile(wms)
download(con, wms, quiet = TRUE, mode = "wb")
basemap <- brick(wms)
names(basemap) <- c("img.1", "img.2", "img.3")
img <- merge(basemap,img)
img@extent@xmin <- xmin
img@extent@ymin <- ymin
img@extent@xmax <- xmax
img@extent@ymax <- ymax
proj4string(img)<-CRS("+proj=longlat +datum=WGS84")
return(img)
}
wms_img<-getWMSmap(wms_layer,xmin,xmax,ymin,ymax)
rggbplot <- function(inRGBRst,npix=NA,scale = 'lin'){
rgblinstretch <- function(rgbDf){
maxList <- apply(rgbDf,2,max)
minList <- apply(rgbDf,2,min)
temp<-rgbDf
for(i in c(1:3)){
temp[,i] <- (temp[,i]-minList[i])/(maxList[i]-minList[i])
}
return(temp)
}
rgbeqstretch<-function(rgbDf){
temp<-rgbDf
for(i in c(1:3)){
unique <- na.omit(temp[,i])
if (length(unique>0)){
ecdf<-ecdf(unique)
temp[,i] <- apply(temp[,i,drop=FALSE],2,FUN=function(x) ecdf(x))
}
}
return(temp)
}
npix <- ncell(inRGBRst)
x <- sampleRegular(inRGBRst, size=npix, asRaster = TRUE)
dat <- as.data.frame(x, xy=TRUE)
colnames(dat)[3:5]<-c('r','g','b')
if(scale=='lin'){
dat[,3:5]<- rgblinstretch(dat[,3:5])
} else if(scale=='stretch'){
dat[,3:5]<- rgbeqstretch(dat[,3:5])
}
p <- ggplot()+ geom_tile(data=dat, aes(x=x, y=y, fill=rgb(r,g,b))) + scale_fill_identity()
}
}
```
```{r, echo=FALSE}
if(nrow(wfs_data) > 0) {
map <- rggbplot(wms_img)+
#borders("worldHires", fill = "gray", colour = "black", xlim = range(xmin,xmax), ylim = range(ymin,ymax), size = .25) +
coord_quickmap(xlim=range(xmin,xmax),ylim=range(ymin,ymax))+
ggtitle(layer_title)+xlab("Longitude")+ylab("Latitude")
plot(map)
} else {
print("No data available for the defined geographical extent")
}
```
</a>
# Litter abundance per year
```{r, echo=FALSE}
if(nrow(wfs_data) > 0) {abundance <- ggplot() +geom_point(data = wfs_data, aes(x = year, y = littercount_by_squarekm, shape=surveyname, group=surveyname ,color=as.character(survey_id)), size=4) +ggtitle("Density per year and location")+xlab("Year")+ylab("Nb. items/km^2")plot(abundance)} else {print("No data available for the defined geographical extent")}
```